package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.TicketNotifySettingDao;
import com.bestcem.xm.ticket.dao.converter.NotifySettingConverter;
import com.bestcem.xm.ticket.dao.mapper.NotifySettingDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.NotifySetting;
import com.bestcem.xm.ticket.entity.mysql.NotifySettingDO;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 公司的通知方式记录表 DAO
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/17 16:39
 */
@Repository
public class TicketNotifySettingDaoImpl implements TicketNotifySettingDao {

    @Resource
    private NotifySettingDaoMapper notifySettingDaoMapper;

    @Override
    public NotifySetting findByOrgId(String orgId) {
        NotifySettingDO notifySettingDO = notifySettingDaoMapper.findByOrgId(orgId);

        return NotifySettingConverter.doToDto(notifySettingDO);
    }

    @Override
    public NotifySetting findByIdAndOrgId(String id, String orgId) {
        NotifySettingDO notifySettingDO = notifySettingDaoMapper.findByNotifySettingId(id);
        if (Objects.nonNull(notifySettingDO) && Objects.nonNull(orgId) && orgId.equals(notifySettingDO.getOrgId())) {
            return NotifySettingConverter.doToDto(notifySettingDO);
        }
        return null;
    }

    @Override
    public NotifySetting saveNotifySetting(NotifySetting notifySetting) {
        NotifySettingDO entityDo = NotifySettingConverter.dtoToDo(notifySetting);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (ObjectUtils.isEmpty(entityDo.getNotifySettingId())) {
            entityDo.setNotifySettingId(ObjectId.next());
        }
        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }
        notifySettingDaoMapper.insert(entityDo);

        return NotifySettingConverter.doToDto(entityDo);
    }

    @Override
    public NotifySetting upsertSetting(String orgId, NotifySetting notifySetting) {
        NotifySettingDO entityDo = NotifySettingConverter.dtoToDo(notifySetting);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        NotifySettingDO settingDO = notifySettingDaoMapper.findByOrgId(orgId);
        if (Objects.isNull(settingDO)) {
            entityDo.setNotifySettingId(ObjectId.next());
            if (Objects.isNull(entityDo.getCreateTime())) {
                entityDo.setCreateTime(DateUtil.getCommonDate());
            }
            if (Objects.isNull(entityDo.getUpdateTime())) {
                entityDo.setUpdateTime(DateUtil.getCommonDate());
            }
            notifySettingDaoMapper.insert(entityDo);
        } else {
            entityDo.setNotifySettingId(settingDO.getNotifySettingId());
            entityDo.setUpdateTime(DateUtil.getCommonDate());

            notifySettingDaoMapper.updateByOrgId(entityDo);
        }

        return NotifySettingConverter.doToDto(entityDo);
    }

    @Override
    public Long deleteEntity(String id) {
        return notifySettingDaoMapper.deleteByNotifySettingId(id);
    }

    @Override
    public Long deleteByIdsAndOrgId(List<String> idList, String orgId) {
        return notifySettingDaoMapper.deleteByIdListAndOrgId(idList, orgId);
    }
}
